using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

using G2007.CodeGen.Library.Generator;

namespace G2007.CodeGen
{

	public class main : System.Windows.Forms.Form
    {
		private System.Windows.Forms.Button btnAbout;
		private System.Windows.Forms.Button btnOpen;
		public System.Windows.Forms.TextBox txtScript;
		private System.Windows.Forms.OpenFileDialog ofdScript;
		private System.Windows.Forms.Button btnCompiler;

		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button btnRun;
        private ProgressBar prgBar;
        private VeryRichTextBox.VRTB vrtbOutput;
        private CheckBox chkShowDebug;
        public CheckBox chkDotNet20;
		private Button btnReEng;
		public CheckBox chkAudit;

		public Settings.Config Config;

		public string AppPath;

		public main()
		{
			Application.EnableVisualStyles();
			InitializeComponent();
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager ( typeof ( main ) );
			this.btnOpen = new System.Windows.Forms.Button ();
			this.txtScript = new System.Windows.Forms.TextBox ();
			this.btnCompiler = new System.Windows.Forms.Button ();
			this.btnRun = new System.Windows.Forms.Button ();
			this.btnAbout = new System.Windows.Forms.Button ();
			this.ofdScript = new System.Windows.Forms.OpenFileDialog ();
			this.prgBar = new System.Windows.Forms.ProgressBar ();
			this.vrtbOutput = new VeryRichTextBox.VRTB ();
			this.chkShowDebug = new System.Windows.Forms.CheckBox ();
			this.chkDotNet20 = new System.Windows.Forms.CheckBox ();
			this.btnReEng = new System.Windows.Forms.Button ();
			this.chkAudit = new System.Windows.Forms.CheckBox ();
			this.SuspendLayout ();
			// 
			// btnOpen
			// 
			this.btnOpen.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnOpen.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.btnOpen.Location = new System.Drawing.Point ( 12, 16 );
			this.btnOpen.Name = "btnOpen";
			this.btnOpen.Size = new System.Drawing.Size ( 67, 20 );
			this.btnOpen.TabIndex = 6;
			this.btnOpen.Text = "&Open...";
			this.btnOpen.Click += new System.EventHandler ( this.onOpen );
			// 
			// txtScript
			// 
			this.txtScript.Anchor = ( ( System.Windows.Forms.AnchorStyles ) ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.txtScript.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.txtScript.Location = new System.Drawing.Point ( 89, 16 );
			this.txtScript.Name = "txtScript";
			this.txtScript.Size = new System.Drawing.Size ( 311, 20 );
			this.txtScript.TabIndex = 5;
			this.txtScript.Text = "<-- Open a script file to generate from";
			// 
			// btnCompiler
			// 
			this.btnCompiler.Anchor = ( ( System.Windows.Forms.AnchorStyles ) ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.btnCompiler.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnCompiler.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.btnCompiler.Location = new System.Drawing.Point ( 299, 42 );
			this.btnCompiler.Name = "btnCompiler";
			this.btnCompiler.Size = new System.Drawing.Size ( 58, 19 );
			this.btnCompiler.TabIndex = 7;
			this.btnCompiler.Text = "&Compile";
			this.btnCompiler.Click += new System.EventHandler ( this.onCompile );
			// 
			// btnRun
			// 
			this.btnRun.Anchor = ( ( System.Windows.Forms.AnchorStyles ) ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right ) ) );
			this.btnRun.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnRun.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.btnRun.Location = new System.Drawing.Point ( 363, 42 );
			this.btnRun.Name = "btnRun";
			this.btnRun.Size = new System.Drawing.Size ( 38, 19 );
			this.btnRun.TabIndex = 8;
			this.btnRun.Text = "&Run";
			this.btnRun.Click += new System.EventHandler ( this.onRun );
			// 
			// btnAbout
			// 
			this.btnAbout.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnAbout.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.btnAbout.Location = new System.Drawing.Point ( 12, 42 );
			this.btnAbout.Name = "btnAbout";
			this.btnAbout.Size = new System.Drawing.Size ( 67, 19 );
			this.btnAbout.TabIndex = 9;
			this.btnAbout.Text = "&About";
			this.btnAbout.Click += new System.EventHandler ( this.onAbout );
			// 
			// ofdScript
			// 
			this.ofdScript.Filter = "All files|*.*";
			this.ofdScript.InitialDirectory = "c:\\";
			this.ofdScript.Title = "Open a codegen script";
			// 
			// prgBar
			// 
			this.prgBar.Location = new System.Drawing.Point ( 12, 90 );
			this.prgBar.Maximum = 12;
			this.prgBar.Name = "prgBar";
			this.prgBar.Size = new System.Drawing.Size ( 388, 23 );
			this.prgBar.TabIndex = 10;
			// 
			// vrtbOutput
			// 
			this.vrtbOutput.Anchor = ( ( System.Windows.Forms.AnchorStyles ) ( ( ( ( System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom )
						| System.Windows.Forms.AnchorStyles.Left )
						| System.Windows.Forms.AnchorStyles.Right ) ) );
			this.vrtbOutput.BackColor = System.Drawing.Color.White;
			this.vrtbOutput.Font = new System.Drawing.Font ( "Sylfaen", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.vrtbOutput.HideSelection = false;
			this.vrtbOutput.Location = new System.Drawing.Point ( 12, 131 );
			this.vrtbOutput.Name = "vrtbOutput";
			this.vrtbOutput.ReadOnly = true;
			this.vrtbOutput.Size = new System.Drawing.Size ( 392, 0 );
			this.vrtbOutput.TabIndex = 11;
			this.vrtbOutput.Text = "";
			this.vrtbOutput.Visible = false;
			// 
			// chkShowDebug
			// 
			this.chkShowDebug.AutoSize = true;
			this.chkShowDebug.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.chkShowDebug.Location = new System.Drawing.Point ( 16, 67 );
			this.chkShowDebug.Name = "chkShowDebug";
			this.chkShowDebug.Size = new System.Drawing.Size ( 58, 20 );
			this.chkShowDebug.TabIndex = 12;
			this.chkShowDebug.Text = "Debug";
			this.chkShowDebug.UseVisualStyleBackColor = true;
			this.chkShowDebug.CheckedChanged += new System.EventHandler ( this.onShowDebug );
			// 
			// chkDotNet20
			// 
			this.chkDotNet20.AutoSize = true;
			this.chkDotNet20.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.chkDotNet20.Location = new System.Drawing.Point ( 80, 67 );
			this.chkDotNet20.Name = "chkDotNet20";
			this.chkDotNet20.Size = new System.Drawing.Size ( 69, 20 );
			this.chkDotNet20.TabIndex = 13;
			this.chkDotNet20.Text = ".NET 2.0";
			this.chkDotNet20.UseVisualStyleBackColor = true;
			// 
			// btnReEng
			// 
			this.btnReEng.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.btnReEng.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.btnReEng.Location = new System.Drawing.Point ( 235, 42 );
			this.btnReEng.Name = "btnReEng";
			this.btnReEng.Size = new System.Drawing.Size ( 56, 20 );
			this.btnReEng.TabIndex = 14;
			this.btnReEng.Text = "Re&Eng";
			this.btnReEng.Click += new System.EventHandler ( this.onReEng );
			// 
			// chkAudit
			// 
			this.chkAudit.AutoSize = true;
			this.chkAudit.Checked = true;
			this.chkAudit.CheckState = System.Windows.Forms.CheckState.Checked;
			this.chkAudit.Font = new System.Drawing.Font ( "Trebuchet MS", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
			this.chkAudit.Location = new System.Drawing.Point ( 155, 67 );
			this.chkAudit.Name = "chkAudit";
			this.chkAudit.Size = new System.Drawing.Size ( 53, 20 );
			this.chkAudit.TabIndex = 15;
			this.chkAudit.Text = "Audit";
			this.chkAudit.UseVisualStyleBackColor = true;
			// 
			// main
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size ( 5, 13 );
			this.BackColor = System.Drawing.Color.Linen;
			this.ClientSize = new System.Drawing.Size ( 412, 126 );
			this.Controls.Add ( this.chkAudit );
			this.Controls.Add ( this.btnReEng );
			this.Controls.Add ( this.chkDotNet20 );
			this.Controls.Add ( this.chkShowDebug );
			this.Controls.Add ( this.vrtbOutput );
			this.Controls.Add ( this.prgBar );
			this.Controls.Add ( this.btnAbout );
			this.Controls.Add ( this.btnRun );
			this.Controls.Add ( this.btnCompiler );
			this.Controls.Add ( this.btnOpen );
			this.Controls.Add ( this.txtScript );
			this.Icon = ( ( System.Drawing.Icon ) ( resources.GetObject ( "$this.Icon" ) ) );
			this.Name = "main";
			this.Text = "CodeGen2007";
			this.FormClosing += new System.Windows.Forms.FormClosingEventHandler ( this.onClosing );
			this.Load += new System.EventHandler ( this.onLoad );
			this.ResumeLayout ( false );
			this.PerformLayout ();

		}
		#endregion


		[STAThread]
		static void Main() 
		{
			Application.Run(new main());
		}


        bool isRegistered()
        {
            if (!System.IO.File.Exists(Application.UserAppDataPath + "\\key.reg"))
            {
                return false;
            }
            else
            {
                StreamReader sr = File.OpenText(Application.UserAppDataPath + "\\key.reg");
               // return keygen.library.Generate.Validate(sr.ReadToEnd().Trim());

				return false;

            }
        }


		private void onLoad(object sender, System.EventArgs e)
		{

            AppPath = Application.StartupPath;

			vrtbOutput.AppendText(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + "\nVersion " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(),Color.Black);
			vrtbOutput.AppendText("http://g2007.com/codegen for help/information/updates\n\n",Color.Black);

			this.txtScript.Text = System.Configuration.ConfigurationSettings.AppSettings[ "DEFAULT_FILE" ];
			Settings.Config.LoadConfig ( this );
		}

		private void onLinkClicked(object sender, System.Windows.Forms.LinkClickedEventArgs e)
		{
			System.Diagnostics.Process.Start(e.LinkText);
		}

		private void onAbout(object sender, System.EventArgs e)
		{
			new About().Show();
		}

		private void onOpen(object sender, System.EventArgs e)
		{
			if(ofdScript.ShowDialog().Equals(DialogResult.OK)) 
			{
				vrtbOutput.AppendText("Open file " + ofdScript.FileName,Color.DarkGreen);
				txtScript.Text=ofdScript.FileName;
				Settings.Config.SaveConfig ( this );
				
			} else  vrtbOutput.AppendText("Open file aborted",Color.Red);
		}


		public void AppendText(object sender, string text, Color color) 
		{
			if ( this.chkShowDebug.Checked ) vrtbOutput.AppendText(sender.GetType().Name + ": " + text,color);
			
		}

		private void onCompile(object sender, System.EventArgs e)
		{
			Settings.Config.SaveConfig ( this );
			vrtbOutput.Clear();
            Compiler compiler = new Compiler ( txtScript.Text , new FileInfo ( Application.ExecutablePath ).DirectoryName );
            compiler.Update += new Compiler.CompilerUpdate ( compilerUpdate );
            compiler.UpdateDetailed += new Compiler.CompilerXML ( compiler_UpdateDetailed );
            prgBar.Value = 0;
			string error;
			if(!compiler.Compile(out error,false, chkAudit.Checked)) vrtbOutput.AppendText("Compile Error " + error,Color.Red);
			else vrtbOutput.AppendText("Compile was successful!");
		}

        void compiler_UpdateDetailed ( string xml )
        {
            if ( this.chkShowDebug.Checked ) this.vrtbOutput.AppendText ( "\n\n\n\n\n\n\n\n" + xml  , Color.DarkBlue );
        }   

        // in the process of updating this to the new value
        void compilerUpdate ( int stage , string stageTxt )
        {
			this.BeginInvoke ( new main.compilerUpdateDel ( this.doCU ), new object[] { stage, stageTxt } );
        }


		void doCU ( int stage, string stageTxt )
		{
			this.prgBar.Value = stage;
		}


		public delegate void compilerUpdateDel ( int stage, string stageTxt );


		private void onRun(object sender, System.EventArgs e)
		{
			Settings.Config.SaveConfig ( this );
			vrtbOutput.Clear();
            Compiler compiler=new Compiler(txtScript.Text,new FileInfo(Application.ExecutablePath).DirectoryName);
            compiler.Update += new Compiler.CompilerUpdate ( compilerUpdate );
            compiler.UpdateDetailed += new Compiler.CompilerXML ( compiler_UpdateDetailed );
            prgBar.Value = 0;
			string error;
			if(!compiler.Compile(out error,true,chkAudit.Checked)) vrtbOutput.AppendText("Compile Error " + error,Color.Red);
			else vrtbOutput.AppendText("Compile was successful!");			
		}

        private void onShowDebug(object sender, EventArgs e)
        {
            vrtbOutput.Visible = chkShowDebug.Checked;
            this.Height = vrtbOutput.Visible ? this.Height + 500 : 170;
        }

		private void onReEng ( object sender , EventArgs e )
		{
			new ReengineerDatabase().Show();
		}

		private void onClosing ( object sender, FormClosingEventArgs e )
		{
			Settings.Config.SaveConfig ( this );
		}
	}
}
